package cn.cimoc.controller;

import cn.cimoc.annotation.ResponseResult;
import cn.cimoc.model.dto.inut.UserLoginInput;
import cn.cimoc.model.dto.output.UserInfo;
import cn.cimoc.service.UserService;
import cn.cimoc.util.HttpStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@RestController
@ResponseResult
@RequestMapping("/users")
@Api(value="User Controller",tags={"用户操作接口"})
public class UserController extends BaseController {
    @Autowired
    UserService service;

    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public void login(String username, String password) {
        // 具体逻辑由security实现
    }

    @ApiOperation(value = "注册接口")
    @RequestMapping("/reg")
    public void register(String username, String password) {
        service.register(username, password, response);
        HttpStatus.success();
    }

    @PreAuthorize("hasAuthority('/user/update')")
    @PostMapping("/password/change")
    public void changePassword(String oldPassword, String newPassword) {
        service.changePassword(oldPassword, newPassword, uid);
        HttpStatus.success();
    }

    @PreAuthorize("hasAuthority('/user/show')")
    @GetMapping("/info/show")
    public UserInfo showInfo() {
        return service.getInfo(uid);
    }

    @PreAuthorize("hasAuthority('/user/update')")
    @PostMapping("/info/change")
    public void changeInfo(String phone, String email, int gender) {
        service.changeInfo(phone, email, gender, uid);
        HttpStatus.success();
    }

    @PreAuthorize("hasAuthority('/user/update')")
    @PostMapping("/avatar/change")
    public String upload(@RequestParam("file") MultipartFile file) {
        return service.changeAvatar(file, uid);
    }

}
